/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Backend.models;

import IO.models.MapsConfig;
import Map.Map;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author insane
 */
public class MapTableModel implements TableModel {

    private Set<TableModelListener> listeners = new HashSet<TableModelListener>();
    private List<Map> maps;

    public MapTableModel(List<Map> maps) {

        this.maps = maps;

    }

    @Override
    public void addTableModelListener(TableModelListener listener) {

        listeners.add(listener);

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if(columnIndex == 3) return Double.class;
        return Integer.class;
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    public int isExist(int i) throws FileNotFoundException, IOException, ClassNotFoundException{
        for(int a = 0; a < MapsConfig.getInstance().getMap().size(); a++){
            if(i == getValueAt(a, 0))
                return i;
        }
        return -1;
    }

    @Override
    public String getColumnName(int columnIndex) {

        switch (columnIndex) {
            case 0:
                return "Уровень";
            case 1:
                return "Ширина";
            case 2:
                return "Высота";
            case 3:
                return "Заполнение";
            case 4:
                return "Ширина комнат";
            case 5:
                return "Высота комнат";
        }

        return "";

    }

    @Override
    public int getRowCount() {
        return maps.size();

    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        
        Map map = null;
        try {
            map = MapsConfig.getInstance().getMap().get(rowIndex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MapTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MapTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (ClassNotFoundException ex) {
            Logger.getLogger(MapTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        switch (columnIndex) {
            case 0:
                return map.getLevel();
            case 1:
                return map.getWidth();
            case 2:
                return map.getHeight();
            case 3:
                return map.getDensity();
            case 4:
                return map.getObjWidth();
            case 5:
                return map.getObjHeight();

        }

        return "";

    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {

        return false;

    }

    @Override
    public void removeTableModelListener(TableModelListener listener) {

        listeners.remove(listener);

    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
    }
    
}
